package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.models.Scanner;

public class ScannerService {
	private DatabaseHelper DBHelper;
	Context context;
	public ScannerService(Context context){
		this.DBHelper = new DatabaseHelper(context);
		this.context = context;
	}
	public List<Scanner> getAllScanner(){
		String sql = "select * from t_scanner_setting";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<Scanner> Source = new ArrayList<Scanner>();
		if(cursor == null){
			return Source;
		}
		while(cursor.moveToNext()){
			Scanner scanner = new Scanner(
					cursor.getInt(0),
					cursor.getInt(1),
					cursor.getString(2),
					cursor.getString(3),
					cursor.getString(4),
					cursor.getInt(5),
					cursor.getInt(6),
					cursor.getString(7),
					cursor.getString(8),
					cursor.getString(9),
					cursor.getString(10),
					cursor.getString(11),
					cursor.getInt(12),
					cursor.getString(13)
					);
			Source.add(scanner);
		}
		cursor.close();
		return Source;
	}
	public String updateScannerItem(Scanner scanner) {
		// TODO Auto-generated method stub
		String sql = "update t_scanner_setting set ";
		sql += "sn_port = " + scanner.getPort() + ",";
		if(scanner.getDeviceName() != null && !scanner.getDeviceName().equals("")){
			sql += "sn_device_name = '" + scanner.getDeviceName() + "',";
		}else{
			sql += "sn_device_name = " + null + ",";
		}
		
		if(scanner.getBaud() != null && !scanner.getBaud().equals("")){
			sql += "sn_baud = '" + scanner.getBaud() + "',";
		}else{
			sql += "sn_baud = " + null + ",";
		}
		if(scanner.getParity() != null && !scanner.getParity().equals("")){
			sql += "sn_parity = '" + scanner.getParity() + "',";
		}else{
			sql += "sn_parity = " + null + ",";
		}
		if(scanner.getDataBits() >= 0){
			sql += "sn_data_bits = " + scanner.getDataBits() + ",";
		}else{
			sql += "sn_data_bits = " + null + ",";
		}
		if(scanner.getStopBits() >= 0){
			sql += "sn_stop_bits = " + scanner.getStopBits() + ",";
		}else{
			sql += "sn_stop_bits = " + null + ",";
		}
		if(scanner.getDisableCmd() != null && !scanner.getDisableCmd().equals("")){
			sql += "sn_disable_cmd = '" + scanner.getDisableCmd() + "',";
		}else{
			sql += "sn_disable_cmd = " + null + ",";
		}
		if(scanner.getEnableCmd() != null && !scanner.getEnableCmd().equals("")){
			sql += "sn_enable_cmd = '" + scanner.getEnableCmd() + "',";
		}else{
			sql += "sn_enable_cmd = " + null + ",";
		}
		if(scanner.getEndChar() != null && !scanner.getEndChar().equals("")){
			sql += "sn_end_char = '" + scanner.getEndChar() + "',";
		}else{
			sql += "sn_end_char = " + null + ",";
		}
		if(scanner.getChkDigit() != null && !scanner.getChkDigit().equals("")){
			sql += "sn_chk_digit = '" + scanner.getChkDigit() + "',";
		}else{
			sql += "sn_chk_digit = " + null + ",";
		}
		if(scanner.getDataFormat() != null && !scanner.getDataFormat().equals("")){
			sql += "sn_data_format = '" + scanner.getDataFormat() + "',";
		}else{
			sql += "sn_data_format = " + null + ",";
		}
		if(scanner.getWeightFormat() >= 0){
			sql += "sn_weight_format = " + scanner.getWeightFormat() + ",";
		}else{
			sql += "sn_weight_format = " + null + ",";
		}
		sql += "sn_using = '" + scanner.getUsing() + "'";
		sql += " where sn_no = " + scanner.getNo();
		String errorString = DBHelper.execSQL(sql);
		return errorString;
	}
	
	public void deleteItem(int no){
		String sql = "Delete from t_scanner_setting where sn_no = " + no;
		DBHelper.execSQL(sql);
	}
	
	public Scanner FindScannerByNo(int no){
		String sql = "select * from t_scanner_setting where sn_no = " + no;
		Cursor cursor = DBHelper.rawQuery(sql, null);
		Scanner scanner = null;
		if(cursor == null){
			return scanner;
		}
		if(cursor.moveToNext()){
			scanner = new Scanner(
					cursor.getInt(0),
					cursor.getInt(1),
					cursor.getString(2),
					cursor.getString(3),
					cursor.getString(4),
					cursor.getInt(5),
					cursor.getInt(6),
					cursor.getString(7),
					cursor.getString(8),
					cursor.getString(9),
					cursor.getString(10),
					cursor.getString(11),
					cursor.getInt(12),
					cursor.getString(13),
					cursor.getString(14),
					cursor.getString(15)
					);
		}
		cursor.close();
		return scanner;
	}
	
	public int getMaxNo(){
		String sql = "select Max(sn_no) from t_scanner_setting";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		int no = 0;
		if(cursor == null){
			DBError.error = "";
			return no;
		}
		while(cursor.moveToNext()){
			no = cursor.getInt(0);
		}
		cursor.close();
		return no;
	}
	
	public String insertItem(Scanner scanner){
		String sql = "Insert into t_scanner_setting values(";
		sql += scanner.getNo() + ",";
		sql += scanner.getPort() + ",";
		if(scanner.getDeviceName() != null && !scanner.getDeviceName().equals("")){
			sql += "'" + scanner.getDeviceName() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getBaud() != null && !scanner.getBaud().equals("")){
			sql += "'" + scanner.getBaud() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getParity() != null && !scanner.getParity().equals("")){
			sql += "'" + scanner.getParity() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getDataBits() >= 0){
			sql += scanner.getDataBits() + ",";
		}else{
			sql += null + ",";
		}
		if(scanner.getStopBits() >= 0){
			sql += scanner.getStopBits() + ",";
		}else{
			sql += null + ",";
		}
		if(scanner.getDisableCmd() != null && !scanner.getDisableCmd().equals("")){
			sql += "'" + scanner.getDisableCmd() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getEnableCmd() != null && !scanner.getEnableCmd().equals("")){
			sql += "'" + scanner.getEnableCmd() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getEndChar() != null && !scanner.getEndChar().equals("")){
			sql += "'" + scanner.getEndChar() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getChkDigit() != null && !scanner.getChkDigit().equals("")){
			sql += "'" + scanner.getChkDigit() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getDataFormat() != null && !scanner.getDataFormat().equals("")){
			sql += "'" + scanner.getDataFormat() + "',";
		}else{
			sql += null + ",";
		}
		if(scanner.getWeightFormat() >= 0){
			sql += scanner.getWeightFormat() + ",";
		}else{
			sql += null + ",";
		}
		sql += "'" + scanner.getUsing() + "',";
		sql += null + "," + null + ")";
		String errorString = DBHelper.execSQL(sql);
		return errorString;
	}
	
}
